package com.example.minefieldkeepercloud.activity.maintenancerepair;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.minefieldkeepercloud.R;
import com.example.minefieldkeepercloud.activity.machineslist.MachineDetailAc;
import com.example.minefieldkeepercloud.adapter.MDapter;
import com.example.minefieldkeepercloud.adapter.Madapter;
import com.example.minefieldkeepercloud.bean.BaseBean;
import com.example.minefieldkeepercloud.bean.CoinBean;
import com.example.minefieldkeepercloud.bean.MacListBean;
import com.example.minefieldkeepercloud.bean.MainrenanRepairBean;
import com.example.minefieldkeepercloud.interfac.ApiHttpCallback;
import com.example.minefieldkeepercloud.network.MyGson;
import com.example.minefieldkeepercloud.network.NetHelperNew;
import com.example.minefieldkeepercloud.util.TimeCountUtil;
import com.example.minefieldkeepercloud.util.TimeUtil;
import com.example.minefieldkeepercloud.util.TitleControl;
import com.example.minefieldkeepercloud.util.ToastUtil;
import com.example.minefieldkeepercloud.view.dialog.DelayeDialog;
import com.example.minefieldkeepercloud.view.dialog.SuccessDialog;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 维修维护详情
 */
public class MainrenanceRepairDetailsActivity extends AppCompatActivity {

    private MainrenanRepairBean.DataBean bean;
    private String factoryID;


    @BindView(R.id.tv_ip)
    TextView tv_ip;
    @BindView(R.id.tv_state)
    TextView tv_state;
    @BindView(R.id.tv_ModelName)
    TextView tv_ModelName;
    @BindView(R.id.tv_FactoryName)
    TextView tv_FactoryName;
    @BindView(R.id.tv_CustomerName)
    TextView tv_CustomerName;
    @BindView(R.id.tv_UpdateUserName)
    TextView tv_UpdateUserName;
    @BindView(R.id.tv_UpdateTime)
    TextView tv_UpdateTime;
    @BindView(R.id.tv_MacAddr)
    TextView tv_MacAddr;
    @BindView(R.id.tv_BrandName)
    TextView tv_BrandName;
    @BindView(R.id.tv_fanxiu)
    TextView tv_fanxiu;
    @BindView(R.id.tv_SerialNumber)
    EditText tv_SerialNumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mainrenance_repair_details);
        ButterKnife.bind(this);
        initTitle();
        InitView();

    }

    private void initTitle() {
        TitleControl titleControl = new TitleControl(this);
        titleControl.setTitle("维修/维护详情");
    }

    private void InitView() {
        bean = (MainrenanRepairBean.DataBean) getIntent().getBundleExtra("bd").getSerializable("data");
        factoryID = getIntent().getBundleExtra("bd").getString("factoryID");

        tv_ip.setText(bean.getIP());
        if (bean.getMachineStatus() == 3) {
            //维修
            tv_state.setTextColor(getResources().getColor(R.color.yebg));
            tv_state.setText("维修");
        }

        if (bean.getMachineStatus() == 6) {
            //维护
            tv_state.setTextColor(getResources().getColor(R.color.system_color));
            tv_state.setText("维护");
        }

        if (bean.getMachineStatus() == 7) {
            //返修
            tv_state.setTextColor(getResources().getColor(R.color.red));
            tv_state.setText("返修");
            tv_fanxiu.setBackground(getResources().getDrawable(R.drawable.btn_broken_bg2));
            tv_fanxiu.setEnabled(false);
            tv_fanxiu.setTextColor(getResources().getColor(R.color.colorLine));
        }


        tv_ModelName.setText("" + bean.getModelName());
        tv_FactoryName.setText("" + bean.getFactoryName());
        tv_BrandName.setText("" + bean.getBrandName());
        tv_CustomerName.setText(bean.getCustomerName() == null ? "无" : bean.getCustomerName());
        tv_SerialNumber.setText(bean.getSerialNumber() == null ? "" : bean.getSerialNumber());
        tv_UpdateUserName.setText(bean.getUpdateUserName() + " ");
        tv_MacAddr.setText(bean.getMacAddr() + " ");
        try {
            tv_UpdateTime.setText(TimeUtil.dealDateFormat(bean.getUpdateTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }


    }

    @OnClick({R.id.tv_fanxiu, R.id.tv_shangjia})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_fanxiu:
                //返修
                updateSubmit(7);
                break;
            case R.id.tv_shangjia:
                //上架
                updateSubmit(1);
                break;


        }
    }

    private void updateSubmit(int type) {
        if (type==7&&"".equals(tv_SerialNumber.getText().toString())) {
            ToastUtil.showToast(this, "请输入序列号");
            return;
        }

        NetHelperNew.GetMacError_Update("" + type, factoryID, bean.getID() + "", tv_SerialNumber.getText().toString(), new ApiHttpCallback(MainrenanceRepairDetailsActivity.this, 1, true) {
            @Override
            public void onSuccess(String data) {
                BaseBean baseBean = MyGson.getInstance().fromJson(data, BaseBean.class);
                if (baseBean.getType() == 1) {

                    new SuccessDialog(MainrenanceRepairDetailsActivity.this, baseBean.getContent(), new SuccessDialog.OnSuccessListener() {
                        @Override
                        public void getResult() {
                            setResult(12,getIntent());
                            finish();
                        }
                    }).show();
                } else if (baseBean.getType() == 2) {
                    new SuccessDialog(MainrenanceRepairDetailsActivity.this, baseBean.getContent(), new SuccessDialog.OnSuccessListener() {
                        @Override
                        public void getResult() {

                        }
                    }).show();

                } else {
                    ToastUtil.showToast(MainrenanceRepairDetailsActivity.this, baseBean.getContent());
                }
            }

            @Override
            public void onError(String msg) {
                super.onError(msg);
                ToastUtil.showToast(MainrenanceRepairDetailsActivity.this, "请求失败");
                finish();
            }
        });
    }
}
